/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.util.regex.*;
import java.io.*;
import java.net.*;
import java.util.*;

/**
 * 
 * @author Administrator
 */
public class Main {

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String[] args) {
		Socket socket = new Socket();
		BufferedReader in = null;
		PrintWriter out = null;
		// char temp[] = new char[256];

		if (DC_Connect(socket, in) == 1)
			System.out.println("Connection established");

		try {
			in = new BufferedReader(new InputStreamReader(socket
					.getInputStream()));
			out = new PrintWriter(socket.getOutputStream(), true);
		} catch (IOException e) {
		}

		DC_Login(socket, in, out);

	}

	public static int DC_Connect(Socket sock, BufferedReader in) {
		try {
			sock
					.connect(new InetSocketAddress("uwdc.circleofcurrent.com",
							411)); // 129.97.247.245, uwdc.circleofcurrent.com
		} catch (UnknownHostException e) {
			System.out.println("Destination host unreachable");
			System.exit(1);
		} catch (IOException e) {
			System.out.println("An I/O error occured");
			System.exit(1);
		}
		return 1;
	}

	public static int DC_Login(Socket sock, BufferedReader in, PrintWriter out) {
		char lock[] = new char[256];
		try {
			while (!in.ready()) {
			} // wait until character is available
			in.read(lock);
			// System.out.println(lock);
		} catch (IOException e) {
			System.out.println("Error");
		}

		// StringTokenizer lock_token = new StringTokenizer(new String(lock),
		// " ");
		// lock_token.nextToken(); // $Lock
		// String lock_str = new String(lock_token.nextToken()); // <Lock>
		// lock_str += "]";

		// Can't use StringTokenizer

		// This part is pretty messy on account of my
		// lack of knowledge of regexs
		String lock_full = new String(lock);
		String lock_strs[] = lock_full.split("^* Pk"); // ^\\s([^(\\sPk=)]+), ^*
														// Pk, ^(?!.*\$Lock).*
														// Pk
		String lock_str = lock_strs[0].substring(lock_strs[0].indexOf(" ") + 1,
				lock_strs[0].length());
		System.out.println(lock_str);
		byte key[] = new byte[256];

		for (int i = 1; i < lock_str.length(); i++) {
			key[i] = (byte) (((byte) lock_str.charAt(i) ^ (byte) lock_str
					.charAt(i - 1)));
		}

		// calculate and modify key
		key[0] = (byte) ((byte) lock_str.charAt(0)
				^ (byte) lock_str.charAt(lock_str.length() - 1)
				^ (byte) lock_str.charAt(lock_str.length() - 2) ^ 5);

		for (int i = 0; i < lock_str.length(); i++) {
			key[i] = (byte) ((((int) key[i] << 4) & 240) | (((int) key[i] >> 4) & 15));
		}

		String goodKey = new String(key);
		goodKey = goodKey.substring(0, lock_str.length());
		goodKey = goodKey.replace(Character.toString((char) 0), "/%DCN000%/");
		goodKey = goodKey.replace(Character.toString((char) 5), "/%DCN005%/");
		goodKey = goodKey.replace(Character.toString((char) 36), "/%DCN036%/");
		goodKey = goodKey.replace(Character.toString((char) 96), "/%DCN096%/");
		goodKey = goodKey.replace(Character.toString((char) 124), "/%DCN124%/");
		goodKey = goodKey.replace(Character.toString((char) 126), "/%DCN126%/");

		goodKey = new StringBuffer(goodKey).insert(0, "$Key ").toString();

		out.println(goodKey + "|$ValidateNick AeroDC|");
		System.out.println(goodKey + "|$ValidateNick AeroDC|");

		char resp[] = new char[4096];

		try {
			while (!in.ready()) {
			} // wait until character is available
			in.read(resp);
			System.out.println("Response is:" + new String(resp));

		} catch (IOException e) {
			System.out.println("Error");
		}

		out
				.println("$GetNickList|$MyINFO $ALL AeroDC Pwnt <AeroDC is the shit>$ $1000$$9010135919133472$|");

		try {
			while (!in.ready()) {
			} // wait until character is available
			in.read(resp);
			System.out.println("Response is:" + new String(resp));

		} catch (IOException e) {
			System.out.println("Error");
		}

		try {
			// do what you want to do before sleeping
			Thread.currentThread().sleep(5000);// sleep for 1000 ms
			// do what you want to do after sleeptig
		} catch (InterruptedException ie) {
			// If this thread was intrrupted by nother thread
		}
		out.flush();
		out.print("<AeroDC> look at my 8PiB share|");
		out.flush();

		try {
			// do what you want to do before sleeping
			Thread.currentThread().sleep(5000);// sleep for 1000 ms
			// do what you want to do after sleeptig
		} catch (InterruptedException ie) {
			// If this thread was intrrupted by nother thread
		}

		// it somehow printed this
		out.flush();
		out.print("<AeroDC> look at my 8 PiB share, Luke|");
		out.flush();

		try {
			while (!in.ready()) {
			} // wait until character is available
			in.read(resp);
			System.out.println("Response is:" + new String(resp));

		} catch (IOException e) {
			System.out.println("Error");
		}

		try {
			// do what you want to do before sleeping
			Thread.currentThread().sleep(900000);// sleep for 1000 ms
			// do what you want to do after sleeptig
		} catch (InterruptedException ie) {
			// If this thread was intrrupted by nother thread
		}

		return 0;
	}

}
